<template>
	<view>
		<view class="ten-line"></view>
		<!-- 头像 -->
		<button class="head-view" type="primary" @click="getHeadImg" style="background-color:transparent;background:transparent;border-color: transparent;">
			<text class="head-txt">头像</text>
			<image class="head-img" :src="headImage" mode="scaleToFill"></image>
		</button>
		<view class="line"></view>
		<!-- 名字 -->
		<view class="item-view" @click="nameClick">
			<text class="item-txt">名字</text>
			<text class="item-desc">{{userData.nickname+userData.nickcname}}</text>
			<image class="item-arrow" src="../../static/right-arrow.png"></image>
		</view>
		<view class="line"></view>
		<!-- 拾亲号 -->
		<view class="item-view" @click="shiqinhaoClick">
			<text class="item-txt">拾亲号</text>
			<text v-if="userData.username" class="item-desc">{{userData.username}}</text>
			<image class="item-arrow" src="../../static/right-arrow.png"></image>
		</view>
		<view class="line"></view>
		<!-- 我的二维码 -->
		<view class="item-view" @click="myQrCodeClick">
			<text class="item-txt">我的二维码</text>
			<image class="my-code" src="../../static/info-my-code.png"></image>
			<image class="item-arrow" src="../../static/right-arrow.png"></image>
		</view>
		<view class="ten-line"></view>
		<!-- 性别 -->
		<view class="item-view">
			<text class="item-txt">性别</text>
			<text class="item-desc">{{userData.gender=='0'?'女':'男'}}</text>
			<image class="item-arrow"></image>
		</view>
		<view class="line"></view>
		<!-- 生日 -->
		<view class="item-view" @click="timeClick" data-item="1">
			<text class="item-txt">生日</text>
			<text v-if="userData.birthtime" class="item-desc">{{userData.birthtime2==1?userData.birthtime1:userData.birthtime}}</text>
			<image class="item-arrow" src="../../static/right-arrow.png"></image>
		</view>
		<view class="line"></view>
		<!-- 结婚纪念日 -->
		<view class="item-view" @click="timeClick" data-item="2">
			<text class="item-txt">结婚纪念日</text>
			<text v-if="userData.wedding" class="item-desc">{{userData.wedding2==1?userData.wedding1:userData.wedding}}</text>
			<image class="item-arrow" src="../../static/right-arrow.png"></image>
		</view>
		<view class="line"></view>
		<!-- 祖籍地 -->
		<view class="item-view" @click="addressClick" data-item="4">
			<text class="item-txt">祖籍地</text>
			<text v-if="userData.ance" class="item-desc">{{userData.ance}}</text>
			<image class="item-arrow" src="../../static/right-arrow.png"></image>
		</view>
		<view class="line"></view>
		<!-- 现居地 -->
		<view class="item-view" @click="addressClick" data-item="5">
			<text class="item-txt">现居地</text>
			<text v-if="userData.location" class="item-desc">{{userData.location}}</text>
			<image class="item-arrow" src="../../static/right-arrow.png"></image>
		</view>
		<view class="line"></view>
		<!-- 签名 -->
		<view class="item-view" @click="descClick">
			<text class="item-txt">签名</text>
			<text v-if="userData.signstr" class="item-desc" style="width: 500rpx;text-align: right;">{{userData.signstr}}</text>
			<image class="item-arrow" src="../../static/right-arrow.png"></image>
		</view>
		<view class="line"></view>
		<!-- 个人简介 -->
		<view class="item-view" @click="bioClick">
			<text class="item-txt">个人简介</text>
			<text v-if="userData.bio" class="item-desc" style="width: 400rpx;text-align: right;">{{userData.bio}}</text>
			<image class="item-arrow" src="../../static/right-arrow.png"></image>
		</view>
		<view class="ten-line" style="height: 100rpx;"></view>
		<view style="display: flex;flex-direction: column;justify-content: center;align-items: center;width: 100%;background-color: #F5F6FA;">
			<view style="display: flex;flex-direction: row;">
				<text style="color: #000000;font-size: 24rpx;font-weight: 24rpx;" @click="goXieYi">《平台使用协议》</text>
				<text style="color: rgba(88, 88, 88, 1);font-size: 24rpx;font-weight: 24rpx;">和</text>
				<text style="color: #000000;font-size: 24rpx;font-weight: 24rpx;" @click="goYinSi">《隐私协议》</text>
			</view>
			<text style="flex: 1;text-align: center;font-size: 20rpx;margin-top: 14rpx;color: rgba(88, 88, 88, 1);">
				Copyright © 2018-2019 \n拾亲网 版权所有 豫ICP备19018083号-1</text>
		</view>
		<view class="ten-line" style="height: 40rpx;" />

		<changeNamePop :isShow="isChangeNameShow" :dataSource="nameData" @returnInputDialog="inputDialogReturn" />
		<choseAddress :isShow="isChoseAddressShow" @returnAddress="addressReturn" />
		<inputDesc :isShow="isInputDescShow" :dataSource="userData.signstr" @returnInputDesc="inputDescReturn" />
		<inputBio :isShow="isInputBioShow" :dataSource="userData.bio" @returnInputBio="inputBioReturn" />
		<changeShiQinHaoPop :isShow="isInputShiqinHao" @returnShiQinHaoPop="returnShiQinHaoPop" />
	</view>
</template>

<script>
	import request from '../index/request.js';
	import changeNamePop from "@/components/change-name-pop/change-name-pop.vue";
	import choseAddress from '../../components/chose-address/chose-address.vue'
	import inputDesc from "@/components/input-desc/input-desc.vue";
	import inputBio from "@/components/input-bio/input-bio.vue";
	import changeShiQinHaoPop from "@/components/change-shiqinhao-pop/change-shiqinhao-pop.vue";

	export default {
		components: {
			changeNamePop,
			choseAddress,
			inputDesc,
			inputBio,
			changeShiQinHaoPop
		},
		data() {
			return {
				xieYiData: [],
				headImage: "",
				userData: null,
				isChangeNameShow: false,
				isChoseAddressShow: false,
				isInputDescShow: false,
				isInputBioShow: false,
				isInputShiqinHao: false,
				addressType: "0",
				timeType: "0",
				nameData: {},
				onPQD: ""
			}
		},
		onUnload:function(){
			uni.$off("selectTimeReturn")
		},
		methods: {
			onShow: function() {
				this.share.url = '/pages/tabBar/home/index?appInviteId=' + this.appUtil.getAppInviteId()
				this.share.shareType = 1
			},
			onLoad: function(options) {
				var that = this
				this.appUtil.saveAppInviteId(options)
				console.log(JSON.stringify(options))
				this.onPQD = options.onPQD
				uni.$on("selectTimeReturn", (data) => {
					that.timeReturn(data)
				})
				this.getUserInfo()
				this.getXieYiData()
			},
			getHeadImg: function() {
				var that = this
				uni.chooseImage({
					count: 1, // 最多可以选择的图片张数，默认9
					sizeType: ['original', 'compressed'], //original 原图，compressed 压缩图，默认二者都有
					sourceType: ['album', 'camera'], //album 从相册选图，camera 使用相机，默认二者都有。如需直接开相机或直接选相册，请只使用一个选项
					success: function(res) {
						uni.uploadFile({
							url: "https://a.shiqin.wang/addons/alioss/index/upload",
							filePath: res.tempFilePaths[0],
							name: 'file',
							success: function(result) {
								if (result.statusCode == 200) {
									let headData = JSON.parse(decodeURI(result.data))
									that.userData.avatar = headData.data.url_s
									that.headImage = headData.data.url_s
									that.modifyUserInfo("avatar", headData.data.url)
								}
							}
						})
					}
				});
			},
			shiqinhaoClick: function() {
				if (this.userData.is_sq == 1) {
					//修改
					this.isInputShiqinHao = true
				} else {
					//跳转修改记录
					uni.navigateTo({
						url:"../shiqinhao-modify-record/index"
					})
				}
			},
			myQrCodeClick:function(){
				uni.navigateTo({
					url:"../my-qr-code/index"
				})
			},
			returnShiQinHaoPop: function(e) {
				this.isInputShiqinHao = e.isclose
				if (e.tag == 'sure') {
					this.changeShiQinHao(e.inputShiQinHao)
				}
			},
			nameClick: function() {
				this.nameData.nickname = this.userData.nickname
				this.nameData.nickcname = this.userData.nickcname
				this.isChangeNameShow = true
			},

			inputDialogReturn: function(event) {
				this.isChangeNameShow = event.isclose
				let data = JSON.parse(JSON.stringify(this.userData));
				if (event.tag === 'sure') {
					data.nickname = event.inputXingKey
					data.nickcname = event.inputMingKey
					this.userData = data
					this.modifyUserInfo("name", data.nickname + "-" + data.nickcname)
				}
			},
			timeClick: function(event) {
				var that = this
				this.timeType = event.currentTarget.dataset.item;
				uni.navigateTo({
					url: "../choose-time/index?isShowInput=1"
				})
			},
			timeReturn: function(event) {
				let data = JSON.parse(JSON.stringify(this.userData));
				console.log("timeReturn:" + JSON.stringify(event))
				switch (this.timeType) {
					case "1":
						//需要上传的时间
						data.birthtime = event.startTime
						if (event.timeType == 0) {
							this.modifyUserInfoData("birthtime", event.startTime, event.dateType)
						} else {
							this.modifyUserInfo("birthtime1", event.startTime)
						}
						break;
					case "2":
						data.wedding = event.startTime
						if (event.timeType == 0) {
							this.modifyUserInfoData("wedding", event.startTime, event.dateType)
						} else {
							this.modifyUserInfo("wedding1", event.startTime)
						}
						break;
					case "3":
						data.jiritime = event.startTime
						if (event.timeType == 0) {
							this.modifyUserInfoData("jiritime", event.startTime, event.dateType)
						} else {
							this.modifyUserInfo("jiritime1", event.startTime)
						}
						break;
				}
				this.userData = data
			},
			addressClick: function(event) {
				this.isChoseAddressShow = true
				this.addressType = event.currentTarget.dataset.item;
			},
			addressReturn: function(event) {
				this.isChoseAddressShow = event.isclose
				if (event.tag) {
					let data = JSON.parse(JSON.stringify(this.userData));
					if (event.address) {
						switch (this.addressType) {
							case "4": //祖籍地
								data.ance = event.address
								this.modifyUserInfo("ance", event.address)
								break;
							case "5": //现居地
								data.location = event.address
								this.modifyUserInfo("location", event.address)
								break;
							case "6": //墓地
								data.location = event.address
								this.modifyUserInfo("location", event.address)
								break;
						}
					} else {
						switch (this.addressType) {
							case "4": //祖籍地
								data.ance = ""
								this.modifyUserInfo("ance", "")
								break;
							case "5": //现居地
								data.location = ""
								this.modifyUserInfo("location", "")
								break;
							case "6": //墓地
								data.location = ""
								this.modifyUserInfo("location", "")
								break;
						}
					}
					this.userData = data
				}
			},
			descClick: function() {
				this.isInputDescShow = true
			},
			inputDescReturn: function(event) {
				console.log(JSON.stringify(event))
				this.isInputDescShow = event.isclose
				let data = JSON.parse(JSON.stringify(this.userData));
				if (event.inputStr) {
					this.modifyUserInfo("signstr", event.inputStr)
					data.signstr = event.inputStr
					this.userData = data
				}
			},
			bioClick: function() {
				this.isInputBioShow = true
			},
			inputBioReturn: function(event) {
				console.log(JSON.stringify(event))
				this.isInputBioShow = event.isclose
				let data = JSON.parse(JSON.stringify(this.userData));
				if (event.inputStr) {
					this.modifyUserInfo("bio", event.inputStr)
					data.bio = event.inputStr
					this.userData = data
				}
			},
			switchChange: function(event) {
				if (event.target.value) {
					this.userData.alives = 0
				} else {
					this.userData.alives = 1
				}
				this.modifyUserInfo("alives", this.userData.alives + "")
			},
			changeShiQinHao: function(value) {
				var that = this
				request.post("api/user/exd_sqmark", {
					username: value
				}).then(data => {
					that.getUserInfo()
				})
			},
			getUserInfo: function() {
				uni.showToast({
					title:"加载中...",
					icon: "loading"
				})
				var that = this
				request.post("api/common/PQD_data", {
					target_flag: this.onPQD
				}).then(data => {
					that.userData = data
					that.headImage = data.avatar
				})
			},
			modifyUserInfo: function(key, value) {
				var that = this
				request.post("api/user/profile_user", {
					PQD_B: this.onPQD,
					set_key: key,
					set_val: value,
					is_lunar: "0"
				}).then(data => {
					that.getUserInfo()
					uni.showToast({
						icon: "none",
						title: "修改成功"
					})
				})
			},
			modifyUserInfoData: function(key, value, time) {
				var that = this
				request.post("api/user/profile_user", {
					PQD_B: this.onPQD,
					set_key: key,
					set_val: value,
					is_lunar: time
				}).then(data => {
					that.getUserInfo()
					uni.showToast({
						icon: "none",
						title: "修改成功"
					})
				})
			},
			getXieYiData: function() {
				var that = this
				request.post("api/common/H5_page", {
					lave: "2"
				}).then(data => {
					that.xieYiData = data.datas
				})
			},
			goXieYi: function() {
				if (this.xieYiData.length > 0) {
					uni.navigateTo({
						url: '/pages/article-details/index?load_url=' +
							encodeURIComponent(this.xieYiData[0].url)+"&title="+this.xieYiData[0].name
					})
				}
			},
			goYinSi: function() {
				if (this.xieYiData.length > 1) {
					uni.navigateTo({
						url: '/pages/article-details/index?load_url=' +
							encodeURIComponent(this.xieYiData[1].url)+"&title="+this.xieYiData[1].name
					})
				}
			}
		}
	}
</script>

<style>
	@import "index.css";
</style>
